#include "stm8l15x.h"
#include <ctype.h>
#include <stdio.h>
#include "unit_instance.h"
#include "usart.h"
#include <stdlib.h>
#include "cmdthread.h"

#include "string.h"
#include "pubset.h"

#include "formatcmd.h"
 
extern strRamData RamData;

char cmd_bitoper (char *s);
char cmd_bget (char *s);
extern char ComData[500];


const u8 PORT_SET[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
const u16 PORT_ODR[] =
  { (u16) (&GPIOA->ODR), (u16) (&GPIOB->ODR), (u16) (&GPIOC->ODR),
(u16) (&GPIOD->ODR), (u16) (&GPIOE->ODR), (u16) (&GPIOF->ODR), (u16) (&GPIOG->ODR),
(u16) (&GPIOH->ODR), (u16) (&GPIOI->ODR) };

const u16 PORT_IDR[] =
  { (u16) (&GPIOA->IDR), (u16) (&GPIOB->IDR), (u16) (&GPIOC->IDR),
(u16) (&GPIOD->IDR), (u16) (&GPIOE->IDR), (u16) (&GPIOF->IDR), (u16) (&GPIOG->IDR),
(u16) (&GPIOH->IDR), (u16) (&GPIOI->IDR) };


const u16 PORT_DDR[] =
  { (u16) (&GPIOA->DDR), (u16) (&GPIOB->DDR), (u16) (&GPIOC->DDR),
(u16) (&GPIOD->DDR), (u16) (&GPIOE->DDR), (u16) (&GPIOF->DDR), (u16) (&GPIOG->DDR),
(u16) (&GPIOH->DDR), (u16) (&GPIOI->DDR) };


const u16 PORT_CR1[] =
  { (u16) (&GPIOA->CR1), (u16) (&GPIOB->CR1), (u16) (&GPIOC->CR1),
(u16) (&GPIOD->CR1), (u16) (&GPIOE->CR1), (u16) (&GPIOF->CR1), (u16) (&GPIOG->CR1),
(u16) (&GPIOH->CR1), (u16) (&GPIOI->CR1) };

const u16 PORT_CR2[] =
  { (u16) (&GPIOA->CR2), (u16) (&GPIOB->CR2), (u16) (&GPIOC->CR2),
(u16) (&GPIOD->CR2), (u16) (&GPIOE->CR2), (u16) (&GPIOF->CR2), (u16) (&GPIOG->CR2),
(u16) (&GPIOH->CR2), (u16) (&GPIOI->CR2) };

char
pinformat (char *cpinnum, char *cport)
{
  if (_incmdtext1[0] != 'p')
    return ERROR;
  if (isdigit (_incmdtext1[2]))
    {
      *cpinnum = _incmdtext1[2] - '0';
      if (*cpinnum > 7)
	return ERROR;

    }
  else
    return ERROR;


  if ((_incmdtext1[1] >= 'a') && (_incmdtext1[1] <= 'i'))
    *cport = _incmdtext1[1] - 'a';
  else
    return ERROR;

  return SUCCESS;
}

char
cmd_bget (char *s)
{


  // struct STCMDLIST *p = stcmdlist;

  const char errmeg[] =
    "bget is fail\r\n  model : bget pc6  , or bres pb0\r\n";
  char showmsg[] = "pxx  ";
	char	 show_idr[]="idr=x ";
	char	 show_odr[]="odr=x ";
	char 	 show_ddr[]="ddr=x   ";
	char	 show_cr1[]="cr1=x ";
	char	 show_cr2[]="cr2=x ";
  char ctemp;
  char *y = ComData;
  u8 cport, cpinnum;
  u16 j;
  u8 cidr, codr, ccr1, ccr2,cddr ;


 for ( j = 0 ; j < sizeof ( ComData ) ; j++)   ComData[j] = 0 ; 
  if (ERROR == pinformat (&cpinnum, &cport))
    {
     printf ( "%s\n", errmeg );
      return SUCCESS;
    }

  ctemp = *((char *) (PORT_ODR[cport]));
  codr = (_Bool) (ctemp & ( PORT_SET[cpinnum]));

  ctemp = *((char *) (PORT_IDR[cport]));
  cidr = (_Bool) (ctemp & ( PORT_SET[cpinnum]));

  ctemp = *((char *) (PORT_CR1[cport]));
  ccr1 = (_Bool) (ctemp & ( PORT_SET[cpinnum]));

  ctemp = *((char *) (PORT_CR2[cport]));
  ccr2 = (_Bool) (ctemp & ( PORT_SET[cpinnum]));
	
	ctemp = *((char *) (PORT_DDR[cport]));
  cddr = (_Bool) (ctemp & ( PORT_SET[cpinnum]));

  showmsg[1] =  (cport +'a' );
  showmsg[2] =  (cpinnum +'0');
  show_idr[4] = (cidr) ? '1' : '0';
  show_odr[4] = (codr) ? '1' : '0';
	if ( cddr ) 
	{ 
		show_ddr[4] = 'o';
		show_ddr[5] = 'u';
		show_ddr[6] = 't';
	}
	else
	{
		show_ddr[4] = 'i';
		show_ddr[5] = 'n';
		show_ddr[6] = ' ';
		
	}
  show_cr1[4] = (ccr1) ? '1' : '0';
  show_cr2[4] = (ccr2) ? '1' : '0';

	strcpy ( ComData , showmsg);
	strcat ( ComData , show_ddr );
	strcat ( ComData,  show_idr);
	strcat ( ComData, show_odr );
	strcat ( ComData ,show_cr1);
	strcat ( ComData ,show_cr2 );
	strcat ( ComData , "\r\n");

printf ( "%s\n" , ComData ) ; 
 
  return SUCCESS;


}

char
cmd_bitoper (char *s)
{

  const char errmeg[] =
    "bset is fail\r\n  model : bset pc6  , or bres pb0\r\n";
  char *y = ComData;
  u8 cport, cpinnum;
  u16 j;

  
  if (ERROR == pinformat (&cpinnum, &cport))
    {
    printf ( "%s\n", errmeg );
      return SUCCESS;
    }


  if (strcmp (_incmdid, "bset") == 0)

    *((char *) (PORT_ODR[cport])) =
      *((char *) (PORT_ODR[cport])) | PORT_SET[cpinnum];
  else
    *((char *) (PORT_ODR[cport])) =
      *((char *) (PORT_ODR[cport])) & (~PORT_SET[cpinnum]);
			
			
			cmd_bget (s);
		
}
